package com.partner.action.withdraw;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.partner.bean.member.Member;
import com.partner.bean.withdraw.Withdraw;
import com.partner.frame.Result;
import com.partner.frame.common.CommonInstans;
import com.partner.frame.common.ConstantsMark;
import com.partner.frame.util.MemberUtil;
import com.partner.service.member.MemberService;
import com.partner.service.withdraw.WithdrawService;

@Controller
@RequestMapping("/withdraw")
public class WithdrawAction {

	@Autowired
	private MemberService memberService;
	@Autowired
	private WithdrawService withdrawService;
	
	@RequestMapping(value = "/list")
	public ModelAndView list(HttpServletRequest request,Withdraw withdraw
			) throws ParseException{
		ModelAndView result = new ModelAndView();

		List<Withdraw> withdrawList = withdrawService.selectAll(withdraw);
		
		result.addObject("withdraw", withdraw);
		result.addObject("withdrawList", withdrawList);
		result.setViewName("/pages/withdraw/list");
		return result;
	}
	
	@RequestMapping(value = "/activationbefore1",method = RequestMethod.GET)
	public ModelAndView activationbefore1(HttpServletRequest request,HttpServletResponse response,Integer id){

		ModelAndView result = new ModelAndView();
		Withdraw data = new Withdraw();
		if(id!=0){
			data = withdrawService.selectByPrimaryId(id);
		}
		result.addObject("data", data);
		result.setViewName("/pages/withdraw/edit1");
		return result;
	}
	
	@RequestMapping(value = "/toExamine",method = RequestMethod.POST)
	@ResponseBody
	public Result toExamine(HttpServletRequest request,HttpServletResponse response,Integer id,Integer status,String content){
		Result result = new Result();
		Withdraw data = new Withdraw();
		data.setId(id);
		data.setStatus(status);
		data.setContent(content);
		data.setUpdatetime(new Date());
		if(status==2){
			Withdraw withdraw = withdrawService.selectByPrimaryId(id);
			Member member = memberService.selectByPrimaryId(withdraw.getMemberid());
			member.setWithdrawintegral(member.getWithdrawintegral()+withdraw.getWithdrawintegral());
			memberService.updateByPrimaryKey(member);
		}
		withdrawService.updateByPrimaryKey(data);
		result.setDesc(ConstantsMark.INFO_UPDATESUCCESS);
		return result;
	}
	
	@RequestMapping(value = "/activationbefore2",method = RequestMethod.GET)
	public ModelAndView activationbefore2(HttpServletRequest request,HttpServletResponse response,Integer id){

		ModelAndView result = new ModelAndView();
		Withdraw data = new Withdraw();
		if(id!=0){
			data = withdrawService.selectByPrimaryId(id);
		}
		result.addObject("data", data);
		result.setViewName("/pages/withdraw/edit2");
		return result;
	}
	
	@RequestMapping(value = "/examine",method = RequestMethod.POST)
	@ResponseBody
	public Result examine(HttpServletRequest request,HttpServletResponse response,
							Integer id,Integer amount,Integer status,String content){
		Result result = new Result();
		if(status==4){ //拒绝
			Withdraw data = new Withdraw();
			data.setId(id);
			data.setCasetime(new Date());
			data.setStatus(status);
			data.setFinalcontent(content);
			withdrawService.updateByPrimaryKey(data);
			Withdraw withdraw = withdrawService.selectByPrimaryId(id);
			Member member = memberService.selectByPrimaryId(withdraw.getMemberid());
			member.setWithdrawintegral(member.getWithdrawintegral()+withdraw.getWithdrawintegral());
			memberService.updateByPrimaryKey(member);
			result.setDesc(ConstantsMark.INFO_UPDATESUCCESS);
		}else if(status==3){ //同意兑付
			Withdraw data = withdrawService.selectByPrimaryId(id);
			data.setCasetime(new Date());
			data.setStatus(status);
			data.setFinalcontent(content);
			data.setTaxpoint(10.0);
			data.setAdjustmentamount(amount);
			data.setTaxation((data.getWithdrawintegral()-amount)*10.0/100);
			data.setPayamount(data.getWithdrawintegral()-amount-(data.getWithdrawintegral()-amount)*10.0/100);
			if(data.getMemberid()!=null){
				Member mem = memberService.selectByPrimaryId(data.getMemberid());
				if(mem!=null){
					/*if(mem.getWithdrawintegral()>data.getWithdrawintegral()){*/
						/*mem.setWithdrawintegral(mem.getWithdrawintegral()-data.getWithdrawintegral());
						memberService.updateByPrimaryKey(mem);*/
						withdrawService.updateByPrimaryKey(data);
						result.setDesc(ConstantsMark.INFO_UPDATESUCCESS);
					/*}else{
						result.setDesc("兑付失败，此人余额不足！");
					}*/
				}else{
					result.setDesc("兑付失败，查无此人！");
				}
			}
		}
		return result;
	}
	
	
	@RequestMapping(value = "/applyAmount",method = RequestMethod.GET)
	public ModelAndView applyAmount(HttpServletRequest request,HttpServletResponse response,Withdraw withdraw){

		ModelAndView result = new ModelAndView();
		result.addObject("id", withdraw.getMemberid());
		result.setViewName("/pages/withdraw/applyAmount");
		return result;
	}
	
	@RequestMapping(value = "/withdraw",method = RequestMethod.POST)
	@ResponseBody
	public Result withdraw(HttpServletRequest request,Withdraw withdraw) throws ParseException{
		Result result = new Result();
		if(withdraw.getMemberid()!=null){
			Member member = memberService.selectByPrimaryId(withdraw.getMemberid());
			if(member.getWithdrawintegral()<withdraw.getWithdrawintegral()){
				result.setDesc("提现失败，余额不足");
			}else{
				withdraw.setStatus(0);
				withdraw.setNickname(member.getName());
				withdraw.setTel(member.getTel());
				withdraw.setMembernum(member.getNum());
				withdrawService.insert(withdraw);
				member.setWithdrawintegral(member.getWithdrawintegral()-withdraw.getWithdrawintegral());
				memberService.updateByPrimaryKey(member);
				result.setDesc("申请成功");
			}
		}else{
			result.setDesc("提现失败");
		}
		return result;
	}
	
	
	@RequestMapping(value = "/list1")
	public ModelAndView list1(HttpServletRequest request,Withdraw withdraw
			) throws ParseException{
		ModelAndView result = new ModelAndView();

		List<Withdraw> withdrawList = withdrawService.selectAll(withdraw);
		
		result.addObject("withdraw", withdraw);
		result.addObject("withdrawList", withdrawList);
		result.setViewName("/pages/withdraw/list1");
		return result;
	}
	
}
